from flet import IconButton, Page, Row, TextField, icons
from flet import Icon, Page, PopupMenuButton, PopupMenuItem, Row, Text, icons
from flet import ElevatedButton, FilePicker,  FilePickerResultEvent
from flet import AppBar,View, colors
import flet as ft
import flet
import math
from dataclasses import field
import time, threading
import config_json
import getpath
import myglobal
import mydb
myglobal.initpath=getpath.getpath()
config_json.read_setting()
db=mydb.MyDb()
class MainW():
    def imp_clicked(self,e):
        self.objs=db.getDataDays(2000)
        self.update_table_up()

    def imp2_clicked(self,e):
        pass
    def pick_files_result(self,e: FilePickerResultEvent):
        print("pick_files_result======")
        print(dir(e),e.path)
        if e.files:
            for f in e.files:
                print(f,dir(f))
            self.selected_files.value =f.path
            self.selected_files.update()
    def button_clicked(self,e):
        self.tb1.value = f"hello flet"
        self.page.update()

    def button2_clicked(self,e):
        print("button2_clicked")
        self.pick_files_dialog.pick_files(
            allow_multiple=False
        )
    def controls(self,page: ft.Page):
        self.pick_files_dialog = FilePicker(on_result=self.pick_files_result)
        self.selected_files = Text(config_json.setting["database"])
        button_select= ElevatedButton(
            "...",
            on_click=self.button2_clicked
        )
        tb1 = ft.TextField()#label="Standard")
        b = ft.ElevatedButton(text="Submit", on_click=self.button_clicked)
        self.objs=[]
        self.objs2=[]
        imp_button=ElevatedButton(
            "导入",
            on_click=self.imp_clicked
        )
        imp2_button=ElevatedButton(
            "重复导入",
            on_click=self.imp2_clicked
        )
        row0=Row([ft.Text("database file:"),self.selected_files,button_select,self.pick_files_dialog])
        row1=Row([imp_button,imp2_button])
        lv = flet.ListView(expand=1, spacing=10, padding=20, auto_scroll=True)
        # lv.controls=self.controls(page)
        # return lv
        self.table_up=ft.DataTable(
                columns=[
                    ft.DataColumn(ft.Text("id")),
                    ft.DataColumn(ft.Text("time")),
                    ft.DataColumn(ft.Text("weight"), numeric=True),
                ],
                rows=[],
            )
        lv.controls.append(self.table_up)
        lv2 = flet.ListView(expand=1, spacing=10, padding=20, auto_scroll=True)
        self.table_down=ft.DataTable(
                columns=[
                    ft.DataColumn(ft.Text("id")),
                    ft.DataColumn(ft.Text("time")),
                    ft.DataColumn(ft.Text("weight"), numeric=True),
                ],
                rows=[]
            )
        lv2.controls.append(self.table_down)
        return[flet.AppBar(title=flet.Text("Animate")),row0,row1,lv,lv2,tb1, b]
    def update_table_up(self):
        rows=[]
        for obj in self.objs:
            rows.append(ft.DataRow(
                        cells=[
                            ft.DataCell(ft.Text(obj.SampleName)),
                            ft.DataCell(ft.Text(obj.SampleId)),
                            ft.DataCell(ft.Text(obj.SampleWeight)),
                        ],
                    ))
        self.table_up.rows=rows
        self.table_up.update()
    def mainView(self,page):
        self.page=page
        return flet.View(
                    "/",
                    self.controls(page),
                )
if __name__=="__main__":
    flet.app(target=main_simple)